import Vue from 'vue'
import App from './App.vue'
import router from './router/index'
import { store } from './store/index'

// 饿了就要饿了么
import Element from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
// style
import '@/style/reset.scss'
import '@/style/public.scss'

// babel-polyfill
import 'babel-polyfill'

// axios
import http from '@/api/index.js'

// 全局方法
import global from '@/utils/global.js'

// 全局组件
import cmsComponents from './components/public/index.js'
import ipt from './components/public/input'
// window.Element = Element
Vue.use(Element)

Vue.prototype.common = global
Vue.use(cmsComponents)
Vue.use(ipt)
Vue.config.productionTip = false
Vue.prototype.$http = function (url, params, errCb = null) {
    if (typeof errCb === 'function') {
        return http(url, params).catch(errCb)
    }
    return http(url, params).then(xhr => {
        // 根据返回的code值来做不同的处理（和后端约定）
        // console.log(response)
        if ((xhr.code || xhr.status) !== 200) return Promise.reject(xhr)
        return xhr
    }).catch(xhr => {
        Element.Message({
            message: xhr.msg || xhr.err,
            type: 'error'
        })
        return Promise.reject(xhr)
    })
}

// 挂载
const vue = new Vue({
    router,
    store,
    render: h => h(App)
}).$mount('#app')

// 请求方法,不带错误回调默认弹窗报错,写了错误回调就不弹窗 ,走传入的回调,then里处理200事件,catch处理异常事件
window.HTTP = vue.$http
